//
//  ImageViewController.m
//  微客
//
//  Created by 汪的一声就哭了！ on 2016/10/28.
//  Copyright © 2016年 刘成. All rights reserved.
//

#import "ImageViewController.h"

@interface ImageViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation ImageViewController
{
    BOOL _time;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor blackColor];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn];
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:flow];
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.contentOffset = CGPointMake(_weizhiInt*self.view.bounds.size.width, 0);
    self.collectionView.pagingEnabled = YES;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"BigImageCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell1"];
    
    flow.itemSize = CGSizeMake(self.view.bounds.size.width, self.collectionView.bounds.size.height);
    flow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flow.minimumInteritemSpacing = 0;
    flow.minimumLineSpacing = 0;
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnAction:)];
    [self.view addGestureRecognizer:tap];
    
    
}
- (void)btnAction:(UITapGestureRecognizer *)tap
{
    _time = YES;
    [NSTimer scheduledTimerWithTimeInterval:0.8 target:self selector:@selector(timeraAction:) userInfo:nil repeats:NO];
}
- (void)timeraAction:(NSTimer *)timer
{
    if (_time == YES) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BigImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
    cell.scrollView.delegate = self;
    //获取网络图片地址
    NSURL *url = [NSURL URLWithString:_imageArr[indexPath.row]];
    
    [cell.imageView sd_setImageWithURL:url];

    cell.label.text = [NSString stringWithFormat:@"%ld/%ld",indexPath.row+1,_imageArr.count];
    cell.imageView.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *zoomTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zoomAct:)];
    zoomTap.numberOfTapsRequired = 2;
    [cell.scrollView addGestureRecognizer:zoomTap];
    return cell;
}
- (void)zoomAct:(UITapGestureRecognizer *)tap
{
    _time = NO;
    [UIView animateWithDuration:0.4 animations:^{
        UIScrollView *scrollView = (UIScrollView *)[tap view];
        if (scrollView.zoomScale > 1.0) {
            scrollView.zoomScale = 1.0;
        }else{
            scrollView.zoomScale = 1.5;
        }
    }];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _imageArr.count;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == self.collectionView) {
        for (UICollectionViewCell *cell in self.collectionView.visibleCells) {
            for (UIScrollView *view in cell.contentView.subviews) {
                if ([view isKindOfClass:[UIScrollView class]]) {
                    view.zoomScale = 1;
                }
            }
        }
    }
}

// 让UIImageView在UIScrollView缩放后居中显示
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    if (self.collectionView.contentSize.width < scrollView.contentSize.width) {
        NSLog(@"%f",scrollView.contentSize.width);
    }
    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?
    (scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?
    (scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;
    UIView *iv = [[scrollView subviews] firstObject];
    iv.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX,
                            scrollView.contentSize.height * 0.5 + offsetY);
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return [[scrollView subviews] firstObject];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{

    if (error == nil) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"成功保存到相册" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
        [alert show];

    }else{

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存失败" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
        [alert show];
    }
        
}
@end
